package com.gitee.wsl.android.ext.device

import android.content.pm.PackageManager
import android.os.Build

fun PackageManager.getInstallSource(
    packageName: String
): InstallSourceInfoCompat {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        val installSourceInfo = getInstallSourceInfo(packageName)
        return InstallSourceInfoCompat(
            originatingPackageName = installSourceInfo.originatingPackageName,
            initiatingPackageName = installSourceInfo.initiatingPackageName,
            installingPackageName = installSourceInfo.installingPackageName,
        )
    } else {
        val installerPackageName = getInstallerPackageName(packageName)
        return InstallSourceInfoCompat(
            originatingPackageName = installerPackageName,
            initiatingPackageName = installerPackageName,
            installingPackageName = installerPackageName
        )
    }
}

data class InstallSourceInfoCompat(
    val originatingPackageName: String?,
    val initiatingPackageName: String?,
    val installingPackageName: String?
)